
define(['ionic', 'controllers'], function () {
    angular.module('U9Mobile.controllers')
        .controller('LoginCtrl', ['$scope', 'UserService', 'ConnectService', 'UtilService', 'ionicU9ActionSheet','Push',
            function ($scope, UserService, ConnectService, UtilService, ionicU9ActionSheet,Push) {
                $scope.data = {
                    account: '',
                    password: '',
                    companyName: '',
                    selected: '',
                    companyList: [],
                    autologin: UserService.getUserParams('autologin'),
                    rememberpsw: UserService.getUserParams('rememberpsw')
                };

                $scope.$on('$ionicView.beforeEnter', init);

                $scope.clearUser = function () {
                    $scope.data.account = '';
                    $scope.data.password = '';
                };
                $scope.pickCompany = function () {
                    var cache = $scope.data.selected;
                    ionicU9ActionSheet.show({
                        scope: $scope,
                        template:
                        '<scroll-selector list="data.companyList" selected="data.selected" display-field="name" value-field="id" delegate-handle="companyList">' +
                        '</scroll-selector>',
                        buttonClicked: function (index) {
                            if (index) {
                                ConnectService.setConnectParam('company',  $scope.data.selected);
                                updateCompanyName();
                            } else {
                                $scope.data.selected = cache;
                            }
                            return true;
                        }
                    });
                };
                $scope.changePswState = function () {
                    if ($scope.data.rememberpsw === false) {
                        $scope.data.autologin = false;
                    }
                    UserService.setUserParams({
                        autologin: $scope.data.autologin,
                        rememberpsw: $scope.data.rememberpsw
                    });
                };
                $scope.changeLoginState = function () {
                    if ($scope.data.autologin === true) {
                        $scope.data.rememberpsw = true;
                    }
                    UserService.setUserParams({
                        autologin: $scope.data.autologin,
                        rememberpsw: $scope.data.rememberpsw
                    });
                };
                $scope.login = function (invalid) {
                    if (invalid) {
                        UtilService.tipsBottom('请输入用户账号或密码');
                        return;
                    }
                    UtilService.showLoading();
                    UserService.login($scope.data).then(function (data) {
                        if (data.status_code === "200" && data.access_token) {
                            Push.setAlis(UserService.getPushConfig());
                            UserService.getOrgList($scope.data.account).then(function (organizations) {
                                if (organizations.length > 0) {
                                    $scope.appData.orgList = organizations;
                                    $scope.appData.current = UserService.getOrg();
                                }
                            }).finally(function () {
                                UtilService.goState('app.home');
                            });
                        } else if (angular.isString(data)) {
                            UtilService.tipsBottom('用户名或密码错误');
                        } else {
                            UtilService.tipsBottom(data.status_message || '登录失败');
                        }
                    }, function (error) {
                        if (error) {
                            UtilService.tipsBottom('登录失败');
                        } else {
                            UtilService.tipsBottom('网络信号弱或数据连接异常！');
                        }
                    }).finally(function () {
                        UtilService.hideLoading();
                    });
                };

                $scope.$on('logout', function () {
                    $scope.data.account = UserService.getUserParams('account');
                    $scope.data.password = UserService.getUserParams('password');
                });

                function init () {
                    ConnectService.getCompanyList(
                        ConnectService.getConnect('address'),
                        ConnectService.getConnect('port')
                    ).then(function (list) {
                        $scope.data.companyList = list;
                        $scope.data.selected = ConnectService.getConnect('company');
                        updateCompanyName();
                    });

                    $scope.data.account = UserService.getUserParams('account');
                    $scope.data.password = UserService.getUserParams('password');
                }
                function updateCompanyName () {
                    var list = $scope.data.companyList;
                    for (var i = 0, len = list.length; i < len; i++) {
                        if (list[i].id === $scope.data.selected) {
                            $scope.data.companyName = list[i].name;
                            break;
                        }
                    }
                }
            }
        ]);
});